<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;



class Person extends Model
{
    //use SoftDeletes;

    //
    protected $table = "persons";
    protected $guarded = [];

    public $min_time;

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = ['published_at', 'created_at', 'deleted_at'];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'avatar',
        'name',
        'id_number',
        'reg_addr',
        'temp_addr',
        'five_saw_time',
        'leave_time',
        'meet_situation',
        'remark',
        'is_the_city',
        'phone',
        'qq',
        'weixin',
        'user_id',
    ];

    /**
     * 与人员时间版本一对多关联
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function times()
    {
        //$this->last_time = $this->last_time();
        return $this->hasMany(PersonTimes::class, 'person_id', 'id');
    }

    /**
     * 获取人员最小时间
     *
     * @return mixed
     */
    public function min_time()
    {
        $this->min_time =  $this->times()->orderBy('id', 'desc')->first();
        if ($this->min_time) {
            $this->min_time = $this->min_time->toArray()['leave_time'];
        } else {
            $this->min_time = $this->five_saw_time;
        }
    }

    /**
     * 软删除
     *
     * @param $ids
     *
     */
    public static function softDelete($ids)
    {
        Person::WhereIn('id', $ids)->update(["deleted_at" => date('Y-m-d H:i:s')]);
    }


}
